<template>
  <div class="app-container">
    <section>
      <div class="contentBox">
        <div class="title">
          <span>{{ $route.query.title }}</span>
        </div>
        <div class="content">
          <el-form
            ref="form"
            :model="form"
            class="form"
            label-width="140px"
            :rules="rules"
          >
            <el-form-item label="会员卡名称：" style="width: 100%" prop="title">
              <el-input v-model="form.title" placeholder="请输入"></el-input>
            </el-form-item>
            <el-form-item label="金额（￥）：" style="width: 100%" prop="price">
              <el-input v-model="form.price"  placeholder="请输入"></el-input>
            </el-form-item>
            <el-form-item
              label="赠送金额（￥）："
              style="width: 100%"
              prop="give_price"
            >
              <el-input
                v-model="form.give_price"
                placeholder="请输入"
               
              ></el-input>
            </el-form-item>
            <!-- <el-form-item
              label="折扣金额："
              style="width: 100%"
            >
              <el-input
                v-model="form.discount"
                placeholder="请输入"
              ></el-input>
            </el-form-item> -->
            <!-- <el-form-item
              label="适用范围："
              style="width: 100%"
              prop="store_ids"
            >
              <el-select
                v-model="scope"
                placeholder="请选择"
                style="width: 250px"
              >
                <el-option label="全部" value="0"></el-option>
                <el-option label="门店" value="1"></el-option>
              </el-select>
            </el-form-item> -->
            <!-- <el-form-item
              label="指定门店："
              style="width: 100%"
              v-if="scope == 1"
            >
              <el-select
                v-model="store_ids"
                placeholder="请选择"
                style="width: 250px"
                multiple
              >
                <el-option
                  v-for="(ele, index) in list"
                  :key="index"
                  :label="ele.title"
                  :value="ele.id"
                ></el-option>
              </el-select>
            </el-form-item> -->
            <el-form-item
              label="会员卡背景图:"
              style="width: 100%"
              ref="img"
              prop="img"
            >
              <vs-uploading
                width="120px"
                @verify="verify('img', $event)"
                v-model="form.img"
              ></vs-uploading>
            </el-form-item>
            <el-form-item label="介绍：" style="width: 100%" prop="content">
              <el-input
                v-model="form.content"
                placeholder="请输入"
                type="textarea"
                rows="4"
              ></el-input>
            </el-form-item>
          </el-form>
          <div style="margin-top: 40px">
            <el-button style="width: 143px" @click="$router.go(-1)"
              >取消</el-button
            >
            <el-button type="primary" style="width: 143px" @click="save"
              >保存</el-button
            >
          </div>
        </div>
      </div>
    </section>
  </div>
</template>
<script>
import VsUploading from "../../components/VsUploading";
export default {
  components: {
    VsUploading,
  },
  data() {
    var url = (rule, value, callback) => {
      console.log(rule, value, callback);
      // 图片验证
      let obj = this.form[rule.field];
      if (Object.prototype.toString.call(obj) == "[object Array]") {
        console.log("@@@@", obj);
        if (!obj.length) return callback(new Error("请上传图片!"));
      } else if (!obj) {
        //hasFmt设置为true/false
        return callback(new Error("请上传图片!"));
      }
      callback();
    };
    return {
      list: [],
      checkList: [],
      form: {
        title: "",
        price: "",
        store_ids: "",
        content: "",
        id: "",
        give_price: "",
        img: "",
      },
      store_ids: "",
      scope: "0",
      rules: {
        img: { required: true, validator: url },
        title: [
          { required: true, message: "请输入会员卡名称", trigger: "blur" },
        ],
        price: [{ required: true, message: "请输入金额", trigger: "blur" }],
        give_price: [
          { required: true, message: "请输入赠送金额", trigger: "blur" },
        ],
        content: [
          { required: true, message: "请输入会员卡介绍", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    if (this.$route.query.id) {
      this.form.id = this.$route.query.id;
      this.getDetails();
    }
    this.getList();
    this.$route.meta.title = this.$route.query.title;
  },
  mounted() {},
  computed:{
        // discount(){
        //     let money
        //     if(this.form.price && this.form.give_price){
        //         money = this.form.price*1 + this.form.give_price*1
        //     }
        //     console.log(this.form.discount);
        //     this.form.discount = money;
        //     return money
        // }
  },
  methods: {
    // discount(){
    //     let money
    //     if(this.form.price && this.form.give_price){
    //         money = ( this.form.price*1 / this.form.give_price*1) / 100
    //     }
    //     console.log(this.form.discount);
    //     this.form.discount = money;
    // },
    //  获取所有门店
    async getList() {
      let { code, data } = await this.$Http.getAllStore();
      this.list = data;
    },
    async getDetails() {
      let { code, data } = await this.$Http.getVip(this.form);
      this.form.content = data.content;
      this.form.title = data.title;
      this.form.price = data.price;
      this.form.give_price = data.give_price;
    //   this.form.discount = data.discount
      this.form.img = data.img
      if (data.store_ids != 0) {
        this.scope = "1";
        let store_ids = data.store_ids.split(",");
        this.store_ids = store_ids.map(Number);
      } else {
        this.scope = "0";
        this.form.store_ids = data.store_ids;
      }
    },
    verify(hasFmt, sta) {
      if (sta) {
        this.$refs[hasFmt].validate();
      } else this.$refs[hasFmt].clearValidate();
    },
    save() {
      
      // if (this.scope == 1 && !this.store_ids.length) {
      //   this.$message.error("请选择适用门店");
      //   return;
      // }
      // if (this.scope == "0") {
      //   this.form.store_ids = "0";
      // } else {
      //   this.form.store_ids = this.store_ids.join(",");
      // }
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          let { code, data } = await this.$Http.setVip(this.form);
          if (code) {
            if (this.form.id) {
              this.$message.success("编辑成功");
            } else {
              this.$message.success("添加成功");
            }
            this.$router.go(-1);
          }
        } else {
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.app-container {
  section {
    .contentBox {
      background: #fff;
      border-radius: 8px;
      padding: 0px 24px 20px;
      min-width: 900px;
      min-height: calc(100vh - 165px);
      .title {
        line-height: 70px;
        font-size: 18px;
        font-weight: 600;
        color: #383b40;
        .el-icon-close {
          cursor: pointer;
        }
      }
    }
    .content {
      max-width: 800px;
      height: 100%;
      .form {
        display: flex;
        flex-wrap: wrap;
        justify-content: space-between;
      }
    }
  }
}
::v-deep {
  .el-input {
    width: 250px !important;
  }

  .el-icon-circle-plus-outline,
  .el-icon-remove-outline {
    color: #999;
    font-size: 26px;
    margin: 0 5px;
  }
  .el-icon-circle-plus-outline {
    margin-left: 20px;
  }
  .el-icon-remove-outline {
    color: #f00;
  }
  .el-form-item__content {
    display: flex;
    align-items: center;
  }
}
</style>